<template>
<!-- 这是搜索商品，返回的页面 -->
  <div id="search">
    <!-- 商品列表展示-->
    <div class="row clearfix" id="searchList">
      <ul class="g-row-ul">
        <li v-for="(item,index) in goodsList" :key="item.goods_id" @click="search(goodsList[index].goods_id)">
          <img :src="url+item.goods_image" alt="" >
          <h4>{{item.title}}</h4>
          <span>{{item.min_price}} 元</span>
        </li>
      </ul>
    </div>
    <!-- 分页 -->
    <div class="pagination" v-show="pagingBool">
      <el-pagination
        background
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-size="pageSize"
        layout="total, prev, pager, next, jumper"
        :total="totals">
      </el-pagination>
    </div>
  </div>
</template>

<script>
export default {
  
  data () {
    return {
      pagingBool: false,   // 是否显示分页
      currentPage: 1, //默认第一页
      pageSize: 1,  //每页显示多少条数据
      totals: 0,   //一共多少条数据
      totalGoodsList: [],     // 搜索返回的总数据列表
      goodsList: [],  // 商品列表数据
      url: "https://mp.uekea.cn/attachment/",   //图片网址拼接
    }
  },
  methods: {
    // 分页监听当前所在页数
    handleCurrentChange(val) {
      var _this=this;
      let arr = [];
      let pageIndex = _this.pageSize * (val-1);  // 每一页开始下标
      let pageNum = _this.pageSize * val;  // 每一页最后下标
      // 从每一页开始下标循环到小于每一页结束下标
      for (let i = pageIndex; i < pageNum; i++) {
        // 判断订单状态数组其中不为空
        if(this.totalGoodsList[i] != undefined) {
          arr.push(this.totalGoodsList[i]);
        } else {
          break;
        }
      }
       // 判断渲染数组长度，是否大于0，有数据才赋值，没有就赋值空，不显示分页
      if(arr.length > 0) {
        _this.goodsList = arr;  // 赋值给渲染数组
        _this.pagingBool = true;      // 隐藏分页组件
      } else {
        _this.goodsList = [];  // 赋值给渲染数组
        _this.pagingBool = false;      // 隐藏分页组件
      }
    },
    // 跳到商品详情页面
    search(i){
      this.$router.push({"path":"./goodsDetail",query: {id: i}})
    }
  },
  created () {
    var _this = this;
    let value = _this.$route.query.searchValue;
    _this.$get(`goods/search/${value}`).then(res => {
      if(res.code == 200) {
        _this.totalGoodsList = res.data;
        // 把第一页显示的数据，添加到页面
        for (let i = 0; i < _this.pageSize; i++) {
          _this.goodsList.push(res.data[i]);
        }
        // 判断查找到的数组，数量是否大于每页显示的数量，要是大于就赋值给分页，一共多少条数据
        if(res.data.length > _this.pageSize) {
          _this.totals = _this.totalGoodsList.length;
          _this.pagingBool = true;
        } else {
          _this.pagingBool = false;
        }
      }
      // else if(res.code == 404) {
      // }
    })
  }
}
</script>

<style lang="less" scoped>
#search {
  min-width: 1190px;
}
#searchList{
  margin: 0px;
  margin: auto;
  margin-top: 190px;
  max-width: 1250px;
  min-width: 1190px;
  padding: 15px;
  background-color: white;
}
.g-row-ul{
  width: 100%;
  display: flex;
  justify-content: space-between;
  flex-wrap: wrap;
  text-align: center;
  margin-bottom: 80px;
  li{
    width: 26%;
    margin-top: 50px;
    transition-duration: 1s;
    img{
      height: 317px;
      width: 100%;
    }
    h4{
        font-size: 17px;
        height: 40px;
        line-height: 40px;
        padding-top: 10px;
        padding-left: 15px;
        padding-right: 15px;
        overflow:hidden;
        text-overflow:ellipsis;
        white-space: nowrap;
        color: black;
        margin: 10px 0px;
      }
    span{
      color: #E2B972;
      display: inline-block;
      margin-bottom: 20px;
      font-size: 16px;
    }
  }
}
.g-row-ul::after {
    content: '';
    display: block;
    width: 26%;
}
.g-row-ul li:hover{
  transform:translateY(-10px);
  transition-duration: 1.5s;
  box-shadow: 0px 8px 12px 3px #4E5454;
}
.pagination{
  width: 100%;
  // margin-top: 35px;
  margin-bottom: 100px;
  text-align: center;
}
/deep/ .el-pagination span:not([class*=suffix]){
  font-size: 15px;
}
/deep/ .el-pagination button{
  height: 30px;
}
/deep/ .el-pagination.is-background .btn-prev{
  min-width: 32px;
}
/deep/ .el-pager li{
  height: 30px;
  line-height: 30px;
  font-size: 15px;
}
/deep/ .el-pagination.is-background .el-pager li{
  min-width: 32px;
}
/deep/ .el-pagination .btn-prev .el-icon{
  font-size: 18px;
}
/deep/ .el-pagination .btn-next .el-icon{
  font-size: 18px;
}
</style>